package com.longxiao.manager.web;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.longxiao.entity.Course;
import com.longxiao.entity.CourseChapter;
import com.longxiao.service.CourseService;
import com.longxiao.vo.DwzResult;
import com.longxiao.vo.PageVo;

@Controller
@RequestMapping(value = "/course")
public class CourseController extends BaseController {
	@Resource
	private CourseService courseService;
	

	private final String courseList = "/course/list";
	
	/**
	 * 查询课程信息
	 * @param level
	 * @param name
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getList",method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getList(
			@RequestParam(value = "level",required = false)String level,
			@RequestParam(value = "name",required = false)String name,
			@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
			HttpServletRequest request) {
		logger.info("查询课程信息：{},{}",level,name);
		ModelAndView mav = new ModelAndView(courseList);
		Course cond = new Course();
		cond.setName(name);
		cond.setLevel(level);
		int count = courseService.countCourseList(cond);
		PageVo<Course> pageInfo = new PageVo<Course>(pageNum, count);
		List<Course> courseList = courseService.getCourseList(cond, pageInfo.getOffset(), pageInfo.getRows());
		
		mav.addObject("totalCount", count);
		mav.addObject("courseList", courseList);
		mav.addObject("currentPage", pageNum);
		
		initParam(request,mav);
		return mav;
	}
	@RequestMapping(value = "toAdd",method = {RequestMethod.GET})
	public String toAdd(HttpServletRequest request) {
		return "course/add";
	}
	
	@RequestMapping(value = "toUpdate",method = {RequestMethod.GET})
	public ModelAndView toUpdate(Integer id,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("course/update");
		mav.addObject("course", courseService.getCourseById(id));
		return mav;
	}
	
	@RequestMapping(value = "toAddChapter",method = {RequestMethod.GET})
	public ModelAndView toAddChapter(Integer courseId,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("coursechapter/add");
		mav.addObject("courseId", courseId);
		return mav;
	}
	@RequestMapping(value = "toUpdateChapter",method = {RequestMethod.GET})
	public ModelAndView toUpdateChapter(
			@RequestParam(value = "id")Integer id,
			@RequestParam(value = "courseId")Integer courseId,HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("coursechapter/update");
		mav.addObject("courseId", courseId);
		mav.addObject("chapter", courseService.getCourseChapterById(id));
		return mav;
	}
	
	/**
	 * 添加课程
	 * @param name
	 * @param level
	 * @param describe
	 * @param chapterNum
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addCourse",method = {RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> addCourse(
			@RequestParam(value = "name",required = false)String name,
			@RequestParam(value = "level",required = false)String level,
			@RequestParam(value = "describe",required = false,defaultValue = "")String describe,
			@RequestParam(value = "chapterNum",required = false,defaultValue = "0")Integer chapterNum,HttpServletRequest request) {
		logger.info("添加课程信息：{}，{}",name,level);
		try {
			Course course = new Course();
			course.setChapterNum(chapterNum);
			course.setCreatedTime(new Date());
			course.setDescri(describe);
			course.setLevel(level);
			course.setModifiedTime(new Date());
			course.setName(name);
			course.setStatus((byte)1);
			boolean res = courseService.addCourse(course);
			if(res) {
				return DwzResult.success();
			}
		} catch (Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
        return DwzResult.fail("处理失败");
	}
	
	@RequestMapping(value = "addCourseChapter",method = {RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> addCourseChapter(
			@RequestParam(value = "courseId",required = false)Integer courseId,
			@RequestParam(value = "name",required = false)String name,
			@RequestParam(value = "busiName",required = false)String busiName,
			@RequestParam(value = "seq",required = false)Integer seq,
			@RequestParam(value = "describe",required = false,defaultValue = "")String describe,
			@RequestParam(value = "fileContent",required = false,defaultValue = "0")MultipartFile fileContent,
			HttpServletRequest request) {
		logger.info("添加课程信息：{}，{}",name,describe);
		CommonsMultipartFile mf = (CommonsMultipartFile) fileContent;
		try {
			CourseChapter chapter = new CourseChapter();
			chapter.setCourseId(courseId);
			chapter.setCreatedTime(new Date());
			chapter.setDescri(describe);
			chapter.setModifiedTime(new Date());
			chapter.setName(name);
			chapter.setSeq(seq);
			chapter.setBusiName(busiName);
			chapter.setStatus((byte)1);
			courseService.addChapter(chapter);
			String url = courseService.uploadCourseChapterFile(mf.getFileItem().getName(), chapter.getId(), mf.getInputStream());
			if(StringUtils.isNotEmpty(url)) {
				logger.info("添加课程成功：{}",chapter.getId());
				return DwzResult.success();
			}
		} catch (Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
        return DwzResult.fail("处理失败");
	}
	
	
	@RequestMapping(value = "updateCourse",method = {RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> updateCourse(
			@RequestParam(value = "id",required = false)Integer id,
			@RequestParam(value = "name",required = false)String name,
			@RequestParam(value = "level",required = false)String level,
			@RequestParam(value = "describe",required = false,defaultValue = "")String describe,
			@RequestParam(value = "chapterNum",required = false,defaultValue = "0")Integer chapterNum,HttpServletRequest request) {
		logger.info("添加课程信息：{}，{}",name,level);
		try {
			Course course = courseService.getCourseById(id);
			course.setChapterNum(chapterNum);
			course.setDescri(describe);
			course.setLevel(level);
			course.setModifiedTime(new Date());
			course.setName(name);
			course.setStatus((byte)1);
			boolean res = courseService.updateCourse(course);
			if(res) {
				return DwzResult.success();
			}
		} catch (Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
        return DwzResult.fail("处理失败");
	}
	
	@RequestMapping(value = "updateCourseChapter",method = {RequestMethod.POST})
	@ResponseBody
	public DwzResult<Void> updateCourseChapter(
			@RequestParam(value = "id",required = false)Integer id,
			@RequestParam(value = "courseId",required = false)Integer courseId,
			@RequestParam(value = "name",required = false)String name,
			@RequestParam(value = "busiName",required = false)String busiName,
			@RequestParam(value = "seq",required = false)Integer seq,
			@RequestParam(value = "describe",required = false,defaultValue = "")String describe,
			@RequestParam(value = "fileContent",required = false,defaultValue = "0")MultipartFile fileContent,
			HttpServletRequest request) {
		logger.info("更新课程信息：{}，{}",name,describe);
		CommonsMultipartFile mf = (CommonsMultipartFile) fileContent;
		try {
			CourseChapter chapter = courseService.getCourseChapterById(id);
			chapter.setCourseId(courseId);
			chapter.setDescri(describe);
			chapter.setModifiedTime(new Date());
			chapter.setName(name);
			chapter.setBusiName(busiName);
			chapter.setSeq(seq);
			chapter.setStatus((byte)1);
			boolean res = courseService.updateChapter(chapter);
			if(mf != null && mf.getSize() > 0) {
				courseService.uploadCourseChapterFile(mf.getFileItem().getName(), chapter.getId(), mf.getInputStream());
			}
			if(res) {
				logger.info("更新课程成功：{}",chapter.getId());
				return DwzResult.success();
			}
		} catch (Exception e) {
			logger.error("系统异常：" + e.getMessage(),e);
			return DwzResult.fail(e.getMessage());
		}
        return DwzResult.fail("处理失败");
	}
	
	@RequestMapping(value = "getChapters",method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView getChapters(Integer courseId,HttpServletRequest request) {
		logger.info("获取章节信息：{}",courseId);
		ModelAndView mav = new ModelAndView("coursechapter/list");
		mav.addObject("course", courseService.getCourseById(courseId));
		mav.addObject("chapterList", courseService.getCourseChapterList(courseId));
		return mav;
	}
}